﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Contract.SaleOut.Record;
using Common.Contract.Warranty.Record;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateWarrantyMasterServiceCommand : ProviderQueueCommand<IProviderFactory, WarrantyMasterRecord, ResponseRecord>
    {
        public UpdateWarrantyMasterServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(WarrantyMasterRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var wtHist = Record;
            var obj = new WarrantyMaster
            {
                Amt = wtHist.Amt,
                CloseFlag = wtHist.CloseFlag.HasValue ? wtHist.CloseFlag.Value.ToBooleanString() : null,
                ClosePeriod = wtHist.ClosePeriod,
                CloseUser = wtHist.CloseUser,
                CreateDate = wtHist.CreateDate,
                EndDate = wtHist.EndDate,
                EndUserCell = wtHist.EndUserCell,
                IncentiveCfmDate = wtHist.IncentiveCfmDate,
                IncentiveCfmFlag = wtHist.IncentiveCfmFlag.HasValue ? wtHist.IncentiveCfmFlag.Value.ToBooleanString() : null,
                IncentiveCfmPeriod = wtHist.IncentiveCfmPeriod,
                IncentiveCfmUser = wtHist.IncentiveCfmUser,
                LastUpdateDate = wtHist.LastUpdateDate,
                Model = wtHist.Model,
                Point = wtHist.Point,
                SellinDate = wtHist.SellinDate,
                SelloutDate = wtHist.SelloutDate,
                SelloutRespMsg = wtHist.SelloutRespMsg,
                SelloutRespType = wtHist.SelloutRespType,
                SelloutTime = wtHist.SelloutTime,
                SerialNo = wtHist.SerialNumber,
                ShopCell = wtHist.ShopCell,
                ShopCode = wtHist.ShopCode,
                SmsYn = wtHist.SmsYn.HasValue ? wtHist.SmsYn.Value.ToBooleanString() : null,
                StartDate = wtHist.StartDate,
                Suffix = wtHist.Suffix,
                TransferFlag = BooleanString.TRUE,
                TransferDate = DateTime.Now,
                SoTransferDate = DateTime.Now,
                SoTransferFlag = BooleanString.TRUE
            };

            var itemCheck = Providers.WarrantyMaster.Retrieve(wtHist.SerialNumber, wtHist.Model, wtHist.ShopCode);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.WarrantyMaster.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.WarrantyMaster.Update(obj);
            }
            return true;
        }
    }
}
